<?php
require_once('../conf/config.php.inc');
require_once(SYBMAS_LIB_PATH.'/liblogin.php.inc');
require_once(SYBMAS_LAYOUT_PATH.'/layout.php.inc');
require_once(SYBMAS_LIB_PATH.'/libsybmas.php.inc');
require_once(SYBMAS_LAYOUT_PATH.'/menu.php.inc');
include('esquema_bd.php.inc');
verificar_inicio_sesion();
?>

<?php
$tables=array('sybmas_accionPermiso','sybmas_modulo','sybmas_opcionModulo','sybmas_permisoObjeto',
			'sybmas_respaldo','sybmas_rolesmodulo','sybmas_rolesOpcionModulo','sybmas_tipoPermiso','sybmas_tipoRespaldo');


function verificarTablas(){

	global $tables;

	$connection=start_sybmas_connection();
	$query="SELECT name FROM ".
	SYBMAS_DATABASE_NAME."..sysobjects ".
			"WHERE name LIKE 'sybmas_%'";

	//echo "$query<br>";
		
	$result=sybase_query($query,$connection);
	echo "<table width='95%' border='1'>";
	echo "<tr><th>Tabla</th><th>Existencia</th></tr>";

	$tables_in_db=array();

	while($data=sybase_fetch_array($result)){
		$tables_in_db[]=$data['name'];
	}

	foreach($tables as $name){
		echo "<tr>";
		echo "<td>$name</td>";
		$index=array_search($name,$tables_in_db);
		if(!is_bool($index)){
			echo "<td><img src='".IMG_URI."/tick.png' alt='Si'></td>";
		}else{
			echo "<td><img src='".IMG_URI."/cross.png' alt='No'></td>";
		}
		echo "</tr>";
	}
	echo "</table>";
}


function regenerarTablas(){

	global $tables;

	$connection=start_sybmas_connection();
	$query="SELECT name FROM ".
	SYBMAS_DATABASE_NAME."..sysobjects ".
			"WHERE name LIKE 'sybmas_%'";

	$result=sybase_query($query,$connection);
	echo "<table width='95%' border='1'>";
	echo "<tr><th>Tabla</th><th>Accion</th><th>Estado</th><th>Mensaje</th></tr>";

	$tables_in_db=array();

	while($data=sybase_fetch_array($result)){
		$tables_in_db[]=$data['name'];
	}

	foreach($tables as $name){
	$index=array_search($name,$tables_in_db);
		if(!is_bool($index)){
			borrarLlaves($connection,$name);
		}
	}
	
	foreach($tables as $name){
		$index=array_search($name,$tables_in_db);
		if(!is_bool($index)){
			borrarTabla($connection,$name);
		}
		crearTabla($connection,$name);
	}

	foreach($tables as $name){
		crearLlaves($connection,$name);
	}
	
	foreach($tables as $name){
		cargarCatalogo($connection,$name);
	}

	echo "</table>";


}

function crearLlaves($conn,$tabla){
	global $ddl_restricciones_add;
	$ddl_restriccion=$ddl_restricciones_add[$tabla];
	foreach($ddl_restriccion as $restriccion){
		if(!empty($restriccion)){
			$result=@sybase_query($restriccion,$conn);
			echo "<tr>";
			echo "<td>$tabla</td>";
			echo "<td>C. Restriccion</td>";
			if($result){
				echo "<td><img src='".IMG_URI."/tick.png' alt='Si'></td>";
				echo  "<td></td>";
			}else{
				echo "<td><img src='".IMG_URI."/cross.png' alt='No'></td>";
				echo  "<td>$restriccion".sybase_get_last_message()."</td>";
			}
			echo "</tr>";
		}
	}
}

function borrarLlaves($conn,$tabla){
	global $ddl_restricciones_drop;
	$ddl_restriccion=$ddl_restricciones_drop[$tabla];
	foreach($ddl_restriccion as $restriccion){
		if(!empty($restriccion)){
			$result=@sybase_query($restriccion,$conn);
			echo "<tr>";
			echo "<td>$tabla</td>";
			echo "<td>B. Restriccion</td>";
			if($result){
				echo "<td><img src='".IMG_URI."/tick.png' alt='Si'></td>";
				echo  "<td></td>";
			}else{
				echo "<td><img src='".IMG_URI."/cross.png' alt='No'></td>";
				echo  "<td>$restriccion".sybase_get_last_message()."</td>";
			}
			echo "</tr>";
		}
	}
}

function crearTabla($conn,$tabla){
	global $ddl_tablas;
	$ddl_tabla=$ddl_tablas[$tabla];
	$result=@sybase_query($ddl_tabla,$conn);
	echo "<tr>";
	echo "<td>$tabla</td>";
	echo "<td>Creacion</td>";
	if($result){
		echo "<td><img src='".IMG_URI."/tick.png' alt='Si'></td>";
		echo  "<td></td>";
	}else{
		echo "<td><img src='".IMG_URI."/cross.png' alt='No'></td>";
		echo  "<td>".sybase_get_last_message()."</td>";
	}
	echo "</tr>";
}

function cargarCatalogo($conn,$tabla){
	global $dml_catalogos;
	$dml_tabla=$dml_catalogos[$tabla];
	foreach($dml_tabla as $dml){
		if(!empty($dml)){
			$result=@sybase_query($dml,$conn);
			echo "<tr>";
			echo "<td>$tabla</td>";
			echo "<td>Ins. Datos</td>";
			if($result){
				echo "<td><img src='".IMG_URI."/tick.png' alt='Si'></td>";
				echo  "<td></td>";
			}else{
				echo "<td><img src='".IMG_URI."/cross.png' alt='No'></td>";
				echo  "<td>".$dml."</td>";
			}
			echo "</tr>";
		}
	}
}

function borrarTabla($conn,$tabla){
	$query="DROP TABLE $tabla ";
	$result=@sybase_query($query,$conn);
	echo "<tr>";
	echo "<td>$tabla</td>";
	echo "<td>Borrado</td>";
	if($result){
		echo "<td><img src='".IMG_URI."/tick.png' alt='Si'></td>";
		echo  "<td></td>";
	}else{
		echo "<td><img src='".IMG_URI."/cross.png' alt='No'></td>";
		echo  "<td>".sybase_get_last_message()."</td>";
	}
	echo "</tr>";
}


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css"
	href="<?php echo LAYOUT_URI?>/sybmasstyle.css">
<script src="<?php echo LAYOUT_URI?>/jquery-1.2.1.min.js"
	type="text/javascript"></script>
<script src="<?php echo LAYOUT_URI?>/menu-collapsed.js"
	type="text/javascript"></script>
<link rel="stylesheet" type="text/css"
	href="<?php echo LAYOUT_URI?>/style.css" >
<link rel="stylesheet" type="text/css"
	href="<?php echo JAVASCRIPT_URI?>/tabs/tabs.css">
<script src="<?php echo JAVASCRIPT_URI?>/tabs/tabs.js"
	type="text/javascript"></script>
<script type="text/javascript"
	src="<?php echo JAVASCRIPT_URI?>/commons.js"></script>
<script src="<?php echo JAVASCRIPT_URI?>/validacionFormularios.js"
	type="text/javascript"></script>

<title>.:: Sybmas - Inicio</title>
<!--[if lt IE 8]>
   <style type="text/css">
   li a {display:inline-block;}
   li a {display:block;}
   </style>
   <![endif]-->
</head>
<body>
<script src="<?php echo JAVASCRIPT_URI?>/tooltip/wz_tooltip.js"
	type="text/javascript"></script>
<div id="dialog"><?php 
if(obtener_nombre_usuario()==SYBASE_SYBMAS_USER){
	foreach($_POST as $key => $value){
		$$key=$value;
		//echo "$key $value <br>";
	}

	switch ($accionTabla){
		case 'Verificar tablas':
			echo "<h1>Verificacion de existencia de tablas</h1>";
			verificarTablas();
			break;
		case 'Crear/Regenerar tablas':
			echo "<h1>Creacion/Regeneracion de tablas</h1>";
			regenerarTablas();
			break;
		default:
			die('Accion no reconocida');
			break;
	}
	?> <?php
}else{
	echo "<h1>Solamente el usuario ".SYBASE_SYBMAS_USER." puede realizar esta operacion";
}
?></div>
</body>
</html>
